<template>
  <div class="main">
    <breadcrumb/>
    <el-card>
      <el-row>
        <el-col>
          <el-input placeholder="请输入搜索关键字" v-model="queryInfo.query" clearable @clear="getGoodsList">
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
          </el-input>
          <el-button type="primary" @click="addGoodsPage">添加商品</el-button>
        </el-col>
      </el-row>

      <el-table :data="goodsList" stripe border>
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name" width="500px"></el-table-column>
        <el-table-column label="商品价格（元）" prop="goods_price"></el-table-column>
        <el-table-column label="商品重量" prop="goods_weight"></el-table-column>
        <el-table-column label="商品数量" prop="goods_number"></el-table-column>
        <el-table-column label="创建时间" prop="add_time">
          <template slot-scope="scope">
            {{scope.row.add_time | dateFormat}}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <!--<el-button type="primary" class="el-icon-edit" size="mini">
            </el-button>-->
            <el-button type="danger" class="el-icon-delete" size="mini" @click="deleteGoods(scope.row.goods_id)">
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="(pageSize) => {
          queryInfo.pagesize = pageSize
          getGoodsList()
        }"
        @current-change="(pageNum) => {
          queryInfo.pagenum = pageNum
          getGoodsList()
        }"
        :current-page="currentPage"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="5"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import Breadcrumb from '@/components/Breadcrumb'
import { getGoodsList, deleteGoods } from '@/api/goods/list'

export default {
  name: 'List',
  components: { Breadcrumb },
  data: () => {
    return {
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 5
      },
      goodsList: [],
      total: 0,
      currentPage: 0
    }
  },
  created () {
    this.getGoodsList()
  },

  methods: {
    // 删除商品
    async deleteGoods (goodsId) {
      const r = await this.$confirm('确定要删除该商品？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (r !== 'confirm') return this.$message.info('已取消删除！')
      const { data: res } = await deleteGoods(goodsId)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.$message.success(res.meta.msg)
      await this.getGoodsList()
    },

    // 点击添加商品
    addGoodsPage () {
      this.$router.push('/goods/add')
    },

    // 获取商品列表
    async getGoodsList () {
      const { data: res } = await getGoodsList(this.queryInfo)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.goodsList = res.data.goods
      this.total = res.data.total
    }
  }
}
</script>
<style lang="less" scoped>
  @import "./index";
</style>
